package controller

import (
	"github.com/gin-gonic/gin"
	"go_code/gin-blog/service"
	"net/http"
)

//主页面
func IndexHandler(c *gin.Context) {
	//1.从service读取文章数据
	articleRecordList, err := service.GetArticleRecordList(0, 15)
	//for _,v := range articleRecordList{
	//	fmt.Println(v)
	//}
	if err != nil {
		c.HTML(http.StatusInternalServerError, "views/500.html", nil)
		return
	}
	//for k,v := range articleRecordList {
	//	fmt.Printf("articleRescordList[%v]=%v\n",k,v)
	//}
	//2.用于在页面显示所有的分类
	categoryList, err := service.GetAllCategoryList()
	if err != nil {
		c.HTML(http.StatusInternalServerError, "views/500.html", nil)
		return
	}
	//for k,v := range categoryList {
	//	fmt.Printf("categoryList[%v]=%v\n",k,v)
	//}
	c.HTML(http.StatusOK, "views/index.html", gin.H{
		"article_list":  articleRecordList,
		"category_list": categoryList,
	})
}
